<?php

/**
 * @version     $Id: ipblacklist.php 3 2010-10-08 17:49:35Z obalyuk87@gmail.com $
 * @package     AdminSafe
 * @subpackage  Components
 * @copyright   Copyright (C) 2010 Oleksandr Balyuk (www.boolcast.com)
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL v2
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
// No direct access
defined('_JEXEC') or die('Restricted access');
require_once JPATH_COMPONENT_ADMINISTRATOR . DS . 'models' . DS . 'possibleusers.php';

/**
 * Description of accesslog
 *
 * @author Oleksandr Balyuk <obalyuk@kahzam.com>
 */
class AdminsafeModelIpblacklist extends AdminsafeModelPossibleusers {

    /**
     * Default Constructor
     * 
     * @param array $config
     */
    public function __construct($config = array()) {
        parent::__construct($config);

        //
        $this->addTable('#__adminsafe_ip_blacklist');
    }

    /**
     * Bans IP Address
     *
     * @param string $ipAddress
     * @return bool
     */
    public function ban($ipAddress) {
        if ($ipAddress) {
            $tbl = & $this->getTable('ipblacklist');
            $status = (bool) $tbl->save(array('ip_address' => $ipAddress));
            if (!$status) {
                $this->setError($tbl->getError());
            }
            return $status;
        }

        return false;
    }

    /**
     * Unbans IP Address
     *
     * @param string $ipAddress
     * @return bool
     */
    public function unban($ipAddress) {
        if ($ipAddress) {
            $tbl = & $this->getTable('ipblacklist');
            if ($tbl->load($ipAddress)) {
                return (bool) $tbl->delete();
            }
        }

        return false;
    }

    /**
     * Saves IP Address
     *
     * @param string $ip
     * @return bool
     */
    public function save($ip) {
        return $this->ban($ip);
    }

    /**
     * Method to delete record(s)
     *
     * @param array $ips array with ips
     * @return bool True on success
     */
    public function delete($ips) {
        //
        return $this->_delete($ips);
    }

    /**
     * Deletes specified records
     *
     * @param array $ips
     * @return bool
     */
    protected function _delete($ips) {

        //
        if (is_array($ips)) {
            //
            $tbl = $this->getTable();
            foreach ($ips as $cid) {
                if ($tbl->load($cid)) {
                    if (!$tbl->delete()) {
                        $this->setError($tbl->getError());
                        return false;
                    }
                }
            }
        }
        return true;
    }

}